//
//  statusCell.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/11.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit
import SnapKit

class statusCell: UITableViewCell {
    
// 定义一个约束对象
    var myConstrain: Constraint?
    
    //定义一个数据模型属性  用来为originalView等设置数据
    var model:status?{
        
        didSet{
        
            originalView.model = model
            
            self.myConstrain?.uninstall()

            if let retWeibo : status = model?.retweeted_status  {
                //有转发微博时
                //设置转发微博不隐藏
                retweetView.hidden = false
                
                //设置转发微博
                retweetView.model = retWeibo
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.myConstrain =  make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
            }else{
                
                //没有转发微博的时候
                retweetView.hidden = true
                
                retweetView.model = nil
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.myConstrain =  make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            

        }
    }

   
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = UIColor.yellowColor()
        
        setupUI()
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(contentView)
            
//            make.height.equalTo(150)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom).offset(statusMagin)
            
            make.left.right.equalTo(originalView)
            
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            
         self.myConstrain =   make.top.equalTo(retweetView.snp_bottom).offset(statusMagin).constraint
            
            make.left.right.equalTo(originalView)
//            make.bottom.equalTo(self.snp_bottom)

            
            make.height.equalTo(44)
        }
        

        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    
    //原创微博
    lazy var originalView : XCOriginalView  = XCOriginalView()
    
    //转发微博
    lazy var retweetView:XCRetweetView = XCRetweetView()
    
    //底部视图
    lazy var bottomView : XCBottomView = XCBottomView()
    
}
